<?php

/**
 * This is the model class for table "item".
 *
 * The followings are the available columns in table 'item':
 * @property string $no
 * @property string $barcode
 * @property string $name
 * @property string $size_and_name
 * @property string $quatity
 * @property integer $price_per_set
 * @property integer $totol_price
 * @property string $status
 * @property string $date_buy
 * @property string $budget_type
 * @property string $group
 * @property string $L_id_location
 * @property string $U_s_id
 *
 * The followings are the available model relations:
 * @property User $uS
 * @property Location $lIdLocation
 */
class Item extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'item';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('no, barcode, name, size_and_name, quatity, price_per_set, totol_price, status, date_buy, budget_type, group, L_id_location, U_s_id', 'required'),
			array('price_per_set, totol_price', 'numerical', 'integerOnly'=>true),
			array('no, barcode', 'length', 'max'=>10),
			array('name, size_and_name, budget_type, group, L_id_location, U_s_id', 'length', 'max'=>100),
			array('quatity', 'length', 'max'=>9),
			array('status', 'length', 'max'=>20),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('no, barcode, name, size_and_name, quatity, price_per_set, totol_price, status, date_buy, budget_type, group, L_id_location, U_s_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'uS' => array(self::BELONGS_TO, 'User', 'U_s_id'),
			'lIdLocation' => array(self::BELONGS_TO, 'Location', 'L_id_location'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'no' => 'ลำดับที่',
			'barcode' => 'มช.วศ.(เลขที่เอกสาร)',
			'name' => 'รายการ',
			'size_and_name' => 'ขนาดและลักษณะ',
			'quatity' => 'จำนวน(หน่วย)',
			'price_per_set' => 'ราคาต่อหน่วย/ชุด',
			'totol_price' => 'ราคารวม(บาท)',
			'status' => 'สภาพ',
			'date_buy' => 'ซื้อเมื่อ(วัน/เดือน/ปี)',
			'budget_type' => 'ประเภทงบประมาณ',
			'group' => 'กลุ่ม',
			'L_id_location' => 'ที่อยู่ของครุภัณฑ์',
			'U_s_id' => 'เลขบัตรประชาชนผู้ดูแล',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('no',$this->no,true);
		$criteria->compare('barcode',$this->barcode,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('size_and_name',$this->size_and_name,true);
		$criteria->compare('quatity',$this->quatity,true);
		$criteria->compare('price_per_set',$this->price_per_set);
		$criteria->compare('totol_price',$this->totol_price);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('date_buy',$this->date_buy,true);
		$criteria->compare('budget_type',$this->budget_type,true);
		$criteria->compare('group',$this->group,true);
		$criteria->compare('L_id_location',$this->L_id_location,true);
		$criteria->compare('U_s_id',$this->U_s_id,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Item the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
